package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 何惠民
 */

@Validated
@Api(tags = "01管理员管理模块")
@Slf4j
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    IAdminService adminService;

    public AdminController() {
        log.debug("创建控制器对象:AdminController");
    }

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理[管理员登录]的请求,参数{}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        //登录成功返回JWT
        return JsonResult.ok(jwt);
    }

    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理[添加管理员]的请求,参数{}",adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        log.debug("添加管理员数据成功");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @ApiImplicitParam(name = "id", value = "管理员id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@Range(min = 1, message = "删除管理员失败,删除的管理员ID无效")
                                       @PathVariable Long id){
        log.debug("开始处理[删除管理员]的请求,参数:{}",id);
        adminService.deleteById(id);
        log.debug("删除Id为{}的管理员成功",id);
        return JsonResult.ok();
    }

    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiImplicitParam(name = "id", value = "管理员id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult setEnable(@Range(min = 1, message = "启用管理员失败,启用的管理员ID无效")
                                    @PathVariable Long id){
        log.debug("开始处理[启用管理员]的请求,参数id={}",id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 320)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiImplicitParam(name = "id", value = "管理员id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult setDisable(@Range(min = 1, message = "禁用管理员失败,禁用的管理员ID无效")
                                     @PathVariable Long id){
        log.debug("开始处理[禁用管理员]的请求,参数id={}",id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }


    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAnyAuthority('/ams/admin/read')")
    @GetMapping("")
    public JsonResult<List<AdminListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理[查询管理员列表]的请求,参数{}",loginPrincipal);
        log.debug("当前登录的当事人:{}",loginPrincipal);
        List<AdminListItemVO> list = adminService.list();
        log.debug("查询管理员列表成功");
        return JsonResult.ok(list);
    }
}
